package com.acwing.partition9;

import java.io.*;

/**
 * @author `RKC`
 * @date 2021/12/1 14:47
 */
public class AC858Prim算法求最小生成树 {

    private static final int INF = 0x3F3F3F3F;

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
        String[] s = reader.readLine().split(" ");
        int n = Integer.parseInt(s[0]), m = Integer.parseInt(s[1]);
        int[][] edges = new int[m][3];
        for (int i = 0; i < m; i++) {
            s = reader.readLine().split(" ");
            int x = Integer.parseInt(s[0]), y = Integer.parseInt(s[1]), z = Integer.parseInt(s[2]);
            edges[i][0] = x;
            edges[i][1] = y;
            edges[i][2] = z;
        }
        int ans = prim(n, edges);
        if (ans == INF) writer.write("impossible\n");
        else writer.write(ans + "\n");
        writer.flush();
    }

    private static int prim(int n, int[][] edges) {
        //朴素版prim一般用邻接矩阵来存储稠密图
        int[][] matrix = new int[n + 1][n + 1];
        int[] distance = new int[n + 1];
        boolean[] visited = new boolean[n + 1];
        for (int i = 1; i <= n; i++) {
            distance[i] = INF;
            for (int j = 1; j <= n; j++) matrix[i][j] = i == j ? 0 : INF;
        }
        for (int[] edge : edges) matrix[edge[0]][edge[1]] = matrix[edge[1]][edge[0]] = Math.min(matrix[edge[0]][edge[1]], edge[2]);
        int answer = 0;
        for (int k = 0; k < n; k++) {
            int u = -1;
            //选出离集合最近的点
            for (int i = 1; i <= n; i++) {
                if (!visited[i] && (u == -1 || distance[u] > distance[i])) u = i;
            }
            //找不到近的存在的点了，不存在最小生成树
            if (k > 0 && distance[u] == INF) return INF;
            if (k > 0) answer += distance[u];
            visited[u] = true;
            //更新u周围点到已经确定的集合的距离
            for (int v = 1; v <= n; v++) distance[v] = Math.min(distance[v], matrix[u][v]);
        }
        return answer;
    }
}
